/*
 * Copyright 2020 lcnap
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pers.lcnap;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Rpc 请求对象
 */
public class RpcRequest {

    private static final AtomicLong atomicLong = new AtomicLong();

    private long id;

    private String service;

    private String method;

    /* 无法处理原始类型参数 */
    private Object[] args;

    private Class<?>[] argTypes;

    public RpcRequest() {
    }

    public RpcRequest(String service, String method, Object[] args, Class<?>[] argTypes) {
        this.service = service;
        this.method = method;
        this.args = args;
        this.argTypes = argTypes;
        this.id = atomicLong.getAndIncrement();
    }

    public RpcRequest(String service, String method, Object[] args) {
        this.service = service;
        this.method = method;
        this.args = args;
        this.argTypes = getArgType(args);
        this.id = atomicLong.getAndIncrement();
    }

    private static Class<?>[] getArgType(Object[] args){
        if (args == null){
            return null;
        }
        Class<?>[] argTypes = new Class<?>[args.length];

        for (int i = 0; i < args.length; i++) {
            argTypes[i] = args[i].getClass();
        }
        return argTypes;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Class<?>[] getArgTypes() {
        return argTypes;
    }

    public void setArgTypes(Class<?>[] argTypes) {
        this.argTypes = argTypes;
    }

    @Override
    public String toString() {
        return "RpcRequest{" +
                "id=" + id +
                ", service='" + service + '\'' +
                ", method='" + method + '\'' +
                ", args=" + Arrays.toString(args) +
                ", argTypes=" + Arrays.toString(argTypes) +
                '}';
    }
}
